package com.t235.gc.user.controller;

import com.t235.gc.AjaxResult;
import com.t235.gc.entity.Users;
import com.t235.gc.user.service.RegService;
import com.t235.gc.user.service.SmsService;
import com.t235.gc.user.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api
@RestController
public class UserController {

    @Resource
    private RegService regService;

    @Resource
    private SmsService smsService;

    @Resource
    private UserService userService;


    @RequestMapping("/sendPhoneCode")
    public AjaxResult sendPhoneCode(String Phone){
        try {
            return AjaxResult.success(regService.sendPhoneCode(Phone));
        }catch (RuntimeException ex){
            return AjaxResult.error();
        }
    }

    @RequestMapping("/login")
    public AjaxResult login(String Phone, String code){
        try {
            if (regService.validatephone(Phone, code)) {
                Users users = userService.queryPhone(Phone);
                if(users != null){
                    return AjaxResult.success(users);
                }else{
                    if(userService.insertUsers(Phone) > 0) {
                        return AjaxResult.success(userService.queryPhone(Phone));
                    }
                }
            }
            return AjaxResult.error("验证码输入错误");
        }catch (Exception ex){
            ex.printStackTrace();
            return AjaxResult.error("验证过程中出现异常!");
        }
    }

}
